package cn.yeziji.forum.basic.pay;

import com.ijpay.core.model.BaseModel;
import com.ijpay.wxpay.model.v3.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import org.checkerframework.checker.units.qual.A;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 统一下单 model
 * @author hwy
 * @since 2023/08/07 2:10
 */
@Data
@AllArgsConstructor
public class UnifiedOrderV3Model extends BaseModel {
    private String appid;
    /**
     * 服务商公众号ID
     */
    private String sp_appid;
    /**
     * 直连商户号
     */
    private String mchid;
    /**
     * 服务商户号
     */
    private String sp_mchid;
    /**
     * 子商户公众号ID
     */
    private String sub_appid;
    /**
     * 子商户号
     */
    private String sub_mchid;
    /**
     * 商品描述
     */
    private String description;
    /**
     * 商户订单号
     */
    private String out_trade_no;
    /**
     * 交易结束时间
     */
    private String time_expire;
    /**
     * 附加数据
     */
    private String attach;
    /**
     * 通知地址
     */
    private String notify_url;
    /**
     * 订单优惠标记
     */
    private String goods_tag;
    /**
     * 结算信息
     */
    private SettleInfo settle_info;
    /**
     * 订单金额
     */
    private Amount amount;
    /**
     * 支付者
     */
    private Payer payer;
    /**
     * 优惠功能
     */
    private Detail detail;
    /**
     * 场景信息
     */
    private SceneInfo scene_info;


    public static UnifiedOrderV3ModelBuilder builder() {
        return new UnifiedOrderV3ModelBuilder();
    }

    public static final class UnifiedOrderV3ModelBuilder {
        private String appid;
        private String sp_appid;
        private String mchid;
        private String sp_mchid;
        private String sub_appid;
        private String sub_mchid;
        private String description;
        private String out_trade_no;
        private String time_expire;
        private String attach;
        private String notify_url;
        private String goods_tag;
        private SettleInfo settle_info;
        private Amount amount;
        private Payer payer;
        private Detail detail;
        private SceneInfo scene_info;

        private UnifiedOrderV3ModelBuilder() {
        }

        public UnifiedOrderV3ModelBuilder appid(String appid) {
            this.appid = appid;
            return this;
        }

        public UnifiedOrderV3ModelBuilder sp_appid(String sp_appid) {
            this.sp_appid = sp_appid;
            return this;
        }

        public UnifiedOrderV3ModelBuilder mchid(String mchid) {
            this.mchid = mchid;
            return this;
        }

        public UnifiedOrderV3ModelBuilder sp_mchid(String sp_mchid) {
            this.sp_mchid = sp_mchid;
            return this;
        }

        public UnifiedOrderV3ModelBuilder sub_appid(String sub_appid) {
            this.sub_appid = sub_appid;
            return this;
        }

        public UnifiedOrderV3ModelBuilder sub_mchid(String sub_mchid) {
            this.sub_mchid = sub_mchid;
            return this;
        }

        public UnifiedOrderV3ModelBuilder description(String description) {
            this.description = description;
            return this;
        }

        public UnifiedOrderV3ModelBuilder out_trade_no(String out_trade_no) {
            this.out_trade_no = out_trade_no;
            return this;
        }

        public UnifiedOrderV3ModelBuilder time_expire(String time_expire) {
            this.time_expire = time_expire;
            return this;
        }

        public UnifiedOrderV3ModelBuilder attach(String attach) {
            this.attach = attach;
            return this;
        }

        public UnifiedOrderV3ModelBuilder notify_url(String notify_url) {
            this.notify_url = notify_url;
            return this;
        }

        public UnifiedOrderV3ModelBuilder goods_tag(String goods_tag) {
            this.goods_tag = goods_tag;
            return this;
        }

        public UnifiedOrderV3ModelBuilder settle_info(SettleInfo settle_info) {
            this.settle_info = settle_info;
            return this;
        }

        public UnifiedOrderV3ModelBuilder amount(Amount amount) {
            this.amount = amount;
            return this;
        }

        /**
         * 赋值金额 单位：元
         *
         * @param total 金额
         */
        public UnifiedOrderV3ModelBuilder total(BigDecimal total) {
            // 1元 = 100 分
            int amountTotal = total.multiply(new BigDecimal(100)).intValue();
            if (this.amount == null) {
                this.amount = new Amount().setTotal(amountTotal)
                    .setCurrency("CNY");
            }
            this.amount.setTotal(amountTotal);
            return this;
        }

        /**
         * 赋值金额 单位：元
         *
         * @param total 金额
         */
        public UnifiedOrderV3ModelBuilder total(String total) {
            // 1元 = 100 分
            int amountTotal = new BigDecimal(total).multiply(new BigDecimal(100)).intValue();
            if (this.amount == null) {
                this.amount = new Amount().setTotal(amountTotal)
                    .setCurrency("CNY");
            }
            this.amount.setTotal(amountTotal);
            return this;
        }

        public UnifiedOrderV3ModelBuilder payer(Payer payer) {
            this.payer = payer;
            return this;
        }

        public UnifiedOrderV3ModelBuilder detail(Detail detail) {
            this.detail = detail;
            return this;
        }

        public UnifiedOrderV3ModelBuilder scene_info(SceneInfo scene_info) {
            this.scene_info = scene_info;
            return this;
        }

        public UnifiedOrderV3Model build() {
            return new UnifiedOrderV3Model(appid, sp_appid, mchid, sp_mchid, sub_appid, sub_mchid, description, out_trade_no, time_expire, attach, notify_url, goods_tag, settle_info, amount, payer, detail, scene_info);
        }
    }
}